<?php
include('includes/functions.inc.php');

function pm2email($msg_ids,$user_id)
{
	global $settings, $db_settings, $lang;
	
	$id_list = "";
	foreach ($msg_ids as &$id) {
	    if(strlen($id_list)>0){
	    	$id_list = $id_list.",";
	    }
	    
	    $id_list = $id_list.$id;
	}
	$id_list = "(".$id_list.")";
	
	$view_msg = mysql_query("SELECT a.*, b.user_name sender FROM ".$db_settings['messages_table']." a
        join ".$db_settings['userdata_table']." b on a.sender_id=b.user_id 
        WHERE (a.receiver_id = ".$user_id." or a.sender_id = ".$user_id.") and a.id in ".$id_list." order by a.id desc");
	
	$i = 0;
	$emailbody = "";
	while($msg = mysql_fetch_array($view_msg))
	{
		$i = $i + 1;
		$sender = $msg['sender'];
		$msg_subject = stripslashes(base64_decode($msg['subject']));
		$message = stripslashes(base64_decode($msg['message']));
		$pm_ts = $msg['pm_ts'];
		$emailbody = $emailbody."\n"."Message #".$i;
		$emailbody = $emailbody."\n"."From: ".$sender;
		$emailbody = $emailbody."\n"."Time: ".$pm_ts;
		$emailbody = $emailbody."\n"."Subject: ".$msg_subject;
		$emailbody = $emailbody."\n"."Message Body: ".$message;
		$emailbody = $emailbody."\n\n"."============================================================================";
		$emailbody = $emailbody."\n";
	}
	
	$email_result = mysql_query("SELECT user_name,user_email FROM ".$db_settings['userdata_table']." WHERE user_id = ".$user_id." LIMIT 1");
	$field = mysql_fetch_array($email_result);
	mysql_free_result($email_result);
	$user_name = $field['user_name'];
	$email = $field['user_email'];
	
	$emailbody = stripslashes($emailbody);
	$header  = "From: ".$settings['forum_name']." <".$settings['forum_email'].">\n";
	$header .= "X-Mailer: PHP/" . phpversion(). "\n";
	$header .= "X-Sender-ip: ".$_SERVER["REMOTE_ADDR"]."\n";
	$header .= "Content-Type: text/plain; charset=".$lang['charset']."\n";
	$header .= "Content-transfer-encoding: 8bit\n";
	$recipient = $user_name." <".$email.">";
	$subject = "Total ".$i." Message(s)";
	if($settings['mail_parameter']!='') @mail($recipient, $subject, $emailbody, $header,$settings['mail_parameter']);
	else @mail($recipient, $subject, $emailbody, $header);

	}

	?>